package puzzle.devtools;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.lwjgl.LWJGLException;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import puzzle.devtools.SlickCanvas;
import puzzle.devtools.SlickCanvasTest;

/** 
 * A simple example of using SlickCanvas and JFrame.
 *
 * @author davedes
 */
public class SlickCanvasTest extends JFrame {
  
  public static void main(String[] args) {
    try {
      new SlickCanvasTest();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  public SlickCanvasTest() throws LWJGLException {
    //set up frame
    super("SlickCanvasTest");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
        
    //content panel holds our components
    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
    
    //throws LWJGLException
    final MySlickCanvas canvas = new MySlickCanvas();
    canvas.setSize(400, 400);
    
    //button panel holds our buttons
    JPanel buttonPanel = new JPanel();
    
    //create buttons and add listeners
    JButton button1 = new JButton("One");
    JButton button2 = new JButton("Two");
    button1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        canvas.msg = "Pressed button one";
      } 
    });
    button2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        canvas.msg = "Pressed button two";
      }
    });
    
    //add buttons to button panel
    buttonPanel.add(button1);
    buttonPanel.add(button2);
    
    //add components to content panel
    content.add(buttonPanel);
    content.add(Box.createRigidArea(new Dimension(0, 5)));
    content.add(canvas);
    
    //add the content to the frame
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(content);
    
    //pack, center and show
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
  }
  
  //extend SlickCanvas to render/update your scene
  public class MySlickCanvas extends SlickCanvas {
    
    String msg = "Canvas Test!";
    int x = 50;
    
    public MySlickCanvas() throws LWJGLException {
      super();
    }
    
    //called to set up resources
    protected void initSystem() {
      //Slick implementation of Canvas.setBackground
      setSlickBackground(Color.gray.darker()); 
    }
    
    //called to render the canvas
    protected void render(Graphics g) {
      g.setColor(Color.white);
      g.drawString(msg, 50, 50);
      g.drawRect(x, 100, 25, 25);
    }
    
    //called to update the canvas
    protected void update(int delta) {
      if (x>getWidth())
        x=0;
      else
        x+=delta*0.1f*1;
    }
  }
}